// @ts-nocheck
/* eslint-disable */
import { ObjectDirective } from 'vue';
// import { addResizeListener, removeResizeListener } from 'element-plus/lib/utils/resize-event';

/**
 * created?: DirectiveHook<T, null, V>;
 * beforeMount?: DirectiveHook<T, null, V>;
 * mounted?: DirectiveHook<T, null, V>;
 * beforeUpdate?: DirectiveHook<T, VNode<any, T>, V>;
 * updated?: DirectiveHook<T, VNode<any, T>, V>;
 * beforeUnmount?: DirectiveHook<T, null, V>;
 * unmounted?: DirectiveHook<T, null, V>;
 * getSSRProps?: SSRDirectiveHook;
*/

// 获取原有属性 ie dom元素.currentStyle 火狐谷歌 window.getComputedStyle(dom元素, null);
const getStyle = (function () {
  if (window.document.currentStyle) {
    return (dom: any, attr: any) => dom.currentStyle[attr];
  }
  return (dom: any, attr: any) => getComputedStyle(dom, null)[attr];
}());

const handleDrag: ObjectDirective<any, any> = {
  mounted(el, binding, vnode) {
    const oHeader = el.parentNode || el.parentElement;
    const oDialog = 
      oHeader.parentNode || oHeader.parentElement;

    oHeader.style.cssText += ';cursor:move;';
    oDialog.style.cssText += ';top:0px;';

    oHeader.onmousedown = (e: any) => {
      // 鼠标按下，计算当前元素距离可视区的距离
      const disX = e.clientX - oHeader.offsetLeft;
      const disY = e.clientY - oHeader.offsetTop;

      const dragDomWidth = oDialog.offsetWidth;
      const dragDomHeight = oDialog.offsetHeight;

      const screenWidth = document.body.clientWidth;
      const screenHeight = document.body.clientHeight;

      const minDragDomLeft = oDialog.offsetLeft;
      const maxDragDomLeft = screenWidth - oDialog.offsetLeft - dragDomWidth;

      const minDragDomTop = oDialog.offsetTop;
      const maxDragDomTop = screenHeight - oDialog.offsetTop - dragDomHeight;

      // 获取到的值带px 正则匹配替换
      let styL = getStyle(oDialog, 'left');
      let styT = getStyle(oDialog, 'top');

      if (styL.includes('%')) {
        styL = +document.body.clientWidth * (+styL.replace(/%/g, '') / 100);
        styT = +document.body.clientHeight * (+styT.replace(/%/g, '') / 100);
      } else {
        styL = +styL.replace(/\px/g, '');
        styT = +styT.replace(/\px/g, '');
      }

      // move
      document.onmousemove = (event: any) => {
        // 通过事件委托，计算移动的距离
        let left = event.clientX - disX;
        let top = event.clientY - disY;

        // 边界处理
        if (-(left) > minDragDomLeft) {
          left = -minDragDomLeft;
        } else if (left > maxDragDomLeft) {
          left = maxDragDomLeft;
        }

        if (-(top) > minDragDomTop) {
          top = -minDragDomTop;
        } else if (top > maxDragDomTop) {
          top = maxDragDomTop;
        }

        // 移动当前元素
        oDialog.style.cssText += `;left:${left + styL}px;top:${top + styT}px;`;

        // emit onDrag event
        // vnode.child.$emit('dragDialog');
      };

      // 移除
      document.onmouseup = () => {
        document.onmousemove = null;
        document.onmouseup = null;
      };
    };
  },
  unmounted() {
    // oHeader.onmousedown = null;
  },
};

export default handleDrag;
